package controller

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Number"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeUtil/ExtendConfigUtils"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
	"go-fox-edge-kernel-manager/utils/PageUtils"
	"strconv"
)

type deviceMapperManageController struct {
}

func (e *deviceMapperManageController) init() {
	httpserver.Server.POST("/device/mapper/entities", e.selectEntityList)
	httpserver.Server.POST("/device/mapper/page", e.selectPageList)
	httpserver.Server.GET("/device/mapper/entity", e.queryEntity)
	httpserver.Server.PUT("/device/mapper/entity", e.updateEntity)

	httpserver.Server.POST("/device/mapper/delete", e.deleteEntityList)
}

func (e *deviceMapperManageController) selectEntityList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.selectList(context, body, false)
}

func (e *deviceMapperManageController) selectPageList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.selectList(context, body, true)
}

func (e *deviceMapperManageController) selectList(context httpserver.Context, body map[string]interface{}, isPage bool) (interface{}, error) {
	entityList := commEntityManager.FindEntityList(edgeEntity.Type.DeviceMapperEntity.Type(), func(value commEntity.IEntity) bool {
		entity := value.(*edgeEntity.DeviceMapperEntity)

		result := true

		if Map.Has(body, "deviceType") {
			result = result && entity.DeviceType == body["deviceType"]
		}
		if Map.Has(body, "manufacturer") {
			result = result && entity.Manufacturer == body["manufacturer"]
		}
		if Map.Has(body, "objectName") {
			result = result && entity.ObjectName == body["objectName"]
		}
		if Map.Has(body, "mapperName") {
			result = result && entity.MapperName == body["mapperName"]
		}
		if Map.Has(body, "mapperMode") {
			result = result && entity.MapperMode == body["mapperMode"]
		}

		return result
	})

	extendConfigEntityList, err := commEntityManager.GetEntityMap(edgeEntity.Type.ExtendConfigEntity.Type())
	if err != nil {
		return nil, err
	}

	mapList := make([]map[string]interface{}, 0)
	if isPage {
		result := PageUtils.GetPageList(entityList, body)
		data := Map.GetOrDefault(result, "data", make(map[string]interface{}))
		mapList = Map.GetOrDefault(data.(map[string]interface{}), "list", make([]map[string]interface{}, 0)).([]map[string]interface{})

		// 补充扩展字段
		ExtendConfigUtils.ExtendMapList(mapList, extendConfigEntityList, edgeEntity.Type.DeviceMapperEntity.Type())

		return result, nil
	} else {
		mapList = commEntity.BuildMapList(entityList)

		// 补充扩展字段
		ExtendConfigUtils.ExtendMapList(mapList, extendConfigEntityList, edgeEntity.Type.DeviceMapperEntity.Type())

		return mapList, nil
	}
}

func (e *deviceMapperManageController) queryEntity(context httpserver.Context) (interface{}, error) {
	str := context.Query("id")
	if str == "" {
		return nil, errors.New("输入的id为null")
	}

	id, err := strconv.ParseInt(str, 10, 64)
	if err != nil {
		return nil, err
	}

	exist := commEntityManager.GetEntityById(edgeEntity.Type.DeviceMapperEntity.Type(), id)
	if exist == nil {
		return nil, errors.New("实体不存在")
	}

	return commEntity.BuildMap(exist), nil
}

func (e *deviceMapperManageController) updateEntity(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.insertOrUpdate(context, body)
}

func (e *deviceMapperManageController) insertOrUpdate(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	list, err := Map.GetValue(body, "list")
	if err != nil {
		return nil, err
	}

	// 简单校验参数
	if Method.HasEmpty(list) {
		return nil, errors.New("参数不能为空: list")
	}

	for _, val := range list.([]interface{}) {
		item := val.(map[string]interface{})

		id := Map.GetInt64(item, "id", 0)
		mapperName := Map.GetString(item, "mapperName", "")
		mapperMode := Map.GetInt(item, "mapperMode", 0)
		extendParam := Map.GetMap(item, "extendParam", make(map[string]interface{}))

		if Method.HasEmpty(mapperName) || Map.HasNull(item, "id", "mapperMode", "extendParam") {
			return nil, errors.New("参数不能为空: id, mapperMode, extendParam, mapperName")
		}

		exist := commEntityManager.GetEntityById(edgeEntity.Type.DeviceMapperEntity.Type(), id)
		if exist == nil {
			return nil, errors.New("找不到该对象")
		}
		entity := exist.(*edgeEntity.DeviceMapperEntity)

		if mapperMode != entity.MapperMode || mapperName != entity.MapperName || Json.BuildJson(extendParam) != Json.BuildJson(entity.ExtendParam) {
			entity.MapperName = mapperName
			entity.MapperMode = mapperMode
			entity.ExtendParam = extendParam

			err := commEntityManager.UpdateEntity(entity)
			if err != nil {
				return nil, err
			}
		}
	}

	return nil, nil
}

func (e *deviceMapperManageController) deleteEntityList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	list, err := Map.GetValue(body, "list")
	if err != nil {
		return nil, err
	}

	// 简单验证
	if Method.HasEmpty(list) {
		return nil, errors.New("参数不能为空:list")
	}

	for _, val := range list.([]interface{}) {
		id := Number.MakeInt64(val)

		exist := commEntityManager.GetEntityById(edgeEntity.Type.DeviceMapperEntity.Type(), id)
		if exist == nil {
			return nil, errors.New("实体不存在")
		}

		err = commEntityManager.DeleteEntity(exist)
		if err != nil {
			return nil, err
		}
	}

	return nil, nil
}
